﻿Public Class perProximoId
    Inherits perPadrao

    Public Function BuscaID(ByVal sCampoChave As String, _
                            ByVal sTabelaOrigem As String, _
                                  Optional ByVal sPrimeiraCondicao As String = "", _
                                  Optional ByVal sSegundaCondicao As String = "") As Long

        Dim sSql As String
        Dim iNovoId As Long

        Try
            sSql = "SELECT Max(" & sCampoChave & ") + 1 AS MaxReg FROM " & sTabelaOrigem

            If sPrimeiraCondicao <> "" Or sSegundaCondicao <> "" Then
                sSql &= " WHERE "
            End If

            If sPrimeiraCondicao <> "" Then
                sSql &= sPrimeiraCondicao
            End If

            If sPrimeiraCondicao <> "" And sSegundaCondicao <> "" Then
                sSql &= " AND "
            End If

            If sSegundaCondicao <> "" Then
                sSql &= sSegundaCondicao
            End If

            iNovoId = MyBase.executarConsultaCampo(sSql)
            If iNovoId = 0 Then
                iNovoId = 1
            End If

            Return iNovoId

        Catch ex As Exception
            Throw New Exception(ex.Message("Ocorreu um erro ao tentar executar a consulta SQL"))
        End Try

    End Function

End Class
